#pragma once

#include <iostream>
#include <vector>
#include <ctemplate/template.h>
#include "oj_model.hpp"
#include "oj_view.hpp"

namespace ns_view
{
    using namespace ns_model;
    using namespace ns_view;
    using namespace ctemplate;

    const std::string template_path = "wwwroot/template_html/";

    class View
    {
    public:
        void ExpandAllQuestionsHtml(const std::vector<Question> qs, std::string &outhtml)
        {
            std::string src_html = template_path + "all_questions.html";
            // 创建数据字典
            TemplateDictionary root("all_questions");
            for (auto &q : qs)
            {
                // 往root添加子字典
                TemplateDictionary *sub = root.AddSectionDictionary("questions_list");
                sub->SetValue("number", q.num);
                sub->SetValue("show_num", q.show_num);
                sub->SetValue("title", q.title);
                sub->SetValue("level", q.level);
            }
            // 获取要渲染的网页  不做删除任何符号的动作
            Template *tpl = Template::GetTemplate(src_html, DO_NOT_STRIP);
            // 开始渲染
            tpl->Expand(&outhtml, &root);
        }
        void ExpandOneQuestioHtml(Question &q, std::string &outhtml)
        {
            std::string src_html = template_path + "question.html";
            // 创建数据字典
            TemplateDictionary root("question");

            root.SetValue("show_num", q.show_num);
            root.SetValue("title", q.title);
            root.SetValue("level", q.level);
            root.SetValue("desc", q.desc);
            root.SetValue("pre_code", q.body);

            // 获取要渲染的网页  不做删除任何符号的动作
            Template *tpl = Template::GetTemplate(src_html, DO_NOT_STRIP);
            // 开始渲染
            tpl->Expand(&outhtml, &root);
        }
    };
}